package com.benyq.networkprogramme.tcp

import android.util.Log
import java.io.OutputStream
import java.net.Socket
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors

/**
 * @author benyq
 * @date 18-9-24 23:12
 * @E-mail： 1520063035@qq.com
 * @description
 */
class TcpSender(ip:String, port:Int) {

    private lateinit var socket: Socket
    private lateinit var out: OutputStream

    private var executorService : ExecutorService = Executors.newSingleThreadExecutor()

    init {
        executorService.execute{
            socket = Socket(ip, port)
            out = socket.getOutputStream()
        }

    }


    fun sendMsg(msg: String){
        executorService.execute {
            out.write(msg.toByteArray())
            out.flush()
            Log.e("susan---send", msg)
        }
    }

    private fun close(){
        out.close()
        if (socket.isInputShutdown){
            socket.shutdownInput()
        }
        if (socket.isOutputShutdown){
            socket.shutdownOutput()
        }
        if (socket.isConnected){
            socket.close()
        }
    }



}